package com.ww.word.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.word.R;
import com.ww.word.adapter.WordAdapter;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.db.WordDbHelper;
import com.ww.word.entity.Word;
import com.ww.word.ui.menu.LeftFragment;

/**
 * 学习页面
 * @version 2013-12-5
 * @author wangwei
 */
public class StudyActivity extends SlideMenuActivity {
	private List<Word> list = new ArrayList<Word>();
	private ViewFlipper page;
	private LinearLayout linearLayout;
	private Button button;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_study, container, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		setTitle(view, LeftFragment.mTitleName);
		page = (ViewFlipper) view.findViewById(R.id.page);
		linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
		button = (Button) view.findViewById(R.id.button);
		getWordList();
	}

	/**
	 * 获取并整理图片链接.
	 */
	private void makeView() {
		int num = Integer.parseInt(new CoreSharedPreferencesHelper(context).getValue("num"));
		for (int i = 0; i < list.size(); i++) {
			View v = getView(i);
			page.addView(v);
			if (i == num - 1) {
				View v2 = getView2(i);
				page.addView(v2);
			}

			if (i == list.size() - 1) {
				View v3 = getView3();
				page.addView(v3);
			}
		}
	}

	private View getView(final int i) {
		WordAdapter w = new WordAdapter(context, list.get(i));
		return w.getStudyView(page, i);
	}

	private View getView2(final int i) {
		View v = LayoutInflater.from(context).inflate(R.layout.activity_turn, null);
		TextView txt = (TextView) v.findViewById(R.id.txt);
		txt.setText(R.string.study_continue);
		Button turn = (Button) v.findViewById(R.id.turn);
		turn.setText(R.string.study_continue);
		turn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (i < list.size() - 1) {
					page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_in));
					page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_out));
					page.showNext();
				}
			}
		});

		Button turn4 = (Button) v.findViewById(R.id.turn4);
		turn4.setVisibility(View.VISIBLE);
		turn4.setText(R.string.study_repeat);
		turn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));
				page.setDisplayedChild(0);
			}
		});

		Button turn3 = (Button) v.findViewById(R.id.turn3);
		turn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (i > 0) {
					page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
					page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));
					page.showPrevious();
				}
			}
		});
		return v;
	}

	private View getView3() {
		View v = LayoutInflater.from(context).inflate(R.layout.activity_turn, null);
		TextView txt = (TextView) v.findViewById(R.id.txt);
		txt.setText(R.string.study_more);
		Button turn = (Button) v.findViewById(R.id.turn);
		turn.setText(R.string.study_repeat);
		turn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));
				page.setDisplayedChild(0);
			}
		});

		Button turn3 = (Button) v.findViewById(R.id.turn3);
		turn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				page.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
				page.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));
				page.showPrevious();
			}
		});
		return v;
	}

	/**
	 * 获取并整理图片链接.
	 */
	private void getWordList() {
		showProgressDialog(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				WordDbHelper h = new WordDbHelper(context);
				list = h.getWordList();
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	// 定义一个Handler，用来异步处理数据
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (list.size() > 0) {
				page.setVisibility(View.VISIBLE);
				linearLayout.setVisibility(View.GONE);
				makeView();
			} else {
				complete();
			}
			hideProgressDialog();
		};
	};

	private void complete() {
		page.setVisibility(View.GONE);
		linearLayout.setVisibility(View.VISIBLE);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				StudyDbHelper w = new StudyDbHelper(context);
				w.deleteStudy();
				getWordList();
			}
		});
	}
}
